package mashibing.class10;

import java.util.Stack;

/**
 * 非递归二叉树排序
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-16
 */
public class Class10_3_UnRecursiveTraversalBT {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int v) {
            value = v;
        }
    }

    // 先序遍历
    public static void pre(Node root) {
        System.out.print("pre-order: ");
        if (root == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            root = stack.pop();
            System.out.print(root.value);
            if (root.right != null) {
                stack.push(root.right);
            }
            if (root.left != null) {
                stack.push(root.left);
            }
        }
        System.out.println();
    }

    // 中序遍历
    public static void in(Node root) {
        System.out.print("in-order: ");
        if (root == null) {
            return;
        }
        // 一直往左遍历，直到左孩子为null
        Node cur = root;
        Stack<Node> stack = new Stack<>();
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                Node pop = stack.pop();
                System.out.print(pop.value);
                cur = pop.right;
            }
        }
        System.out.println();
    }

    // 后序遍历
    public static void pos1(Node root) {
        System.out.print("pos-order: ");
        if (root == null) {
            return;
        }
        Stack<Node> stack1 = new Stack<>();
        stack1.push(root);
        Stack<Node> stack2 = new Stack<>();
        while (!stack1.isEmpty()) {
            // 头右左
            Node pop = stack1.pop();
            stack2.push(pop);
            if (pop.left != null) {
                stack1.push(pop.left);
            }
            if (pop.right != null) {
                stack1.push(pop.right);
            }
        }
        while (!stack2.isEmpty()) {
            Node pop = stack2.pop();
            System.out.print(pop.value);
        }
        System.out.println();
    }

    // 后续遍历(只用一个stack完成)：看不懂
    public static void pos2(Node h) {
        System.out.print("pos-order: ");
        if (h != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.push(h);
            Node c = null;
            while (!stack.isEmpty()) {
                c = stack.peek();
                if (c.left != null && h != c.left && h != c.right) {
                    stack.push(c.left);
                } else if (c.right != null && h != c.right) {
                    stack.push(c.right);
                } else {
                    System.out.print(stack.pop().value + " ");
                    h = c;
                }
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        pre(head);
        System.out.println("========");
        in(head);
        System.out.println("========");
        pos1(head);
        System.out.println("========");
        pos2(head);
        System.out.println("========");
    }
}
